package com.lkbaby.controller;

import com.lkbaby.pojo.Dept;
import com.lkbaby.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DeptController {

	@Autowired
	private DeptService deptService;

	@GetMapping("/dept/get/{id}")
	// 设置熔断，失败后的回调
	@HystrixCommand(fallbackMethod = "queryByNoHystrix")
	public Dept queryByNo(@PathVariable("id") Long id) {
		Dept dept = deptService.queryDeptByID(id);
		if (dept == null) {
			throw new RuntimeException("id: " + id + "找不到对应信息！");
		}
		return dept;
	}

	// 设置备选方案
	public Dept queryByNoHystrix(@PathVariable("id") Long id) {
		return new Dept()
				.setDeptno(id)
				.setDname("找不到id: " + id + "对应信息")
				.setDbSource("no database");
	}

}
